//
//  CNShopBuyView.m
//  BeeQuick_19
//
//  Created by w on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNShopBuyView.h"
#import "CNGoodsModel.h"
#import "CNShoppingViewController.h"
#import "CNShopCarCell.h"
#import "CNTabBarController.h"
#import "CNNavViewController.h"
#import "CNShoppingViewController.h"
#import "CNShopCarTableFooterView.h"


@interface CNShopBuyView()
    /** 加号按钮 */
@property (nonatomic,strong)UIButton *addGoodsButton;
    /** 减号按钮 */
@property (nonatomic,strong)UIButton *reduceGoodsButton;
    /** 数量label */
@property (nonatomic,strong)UILabel *countLabel;
    /** 库存没货label */
@property (nonatomic,strong)UILabel *supplementLabel;
    /** 购买商品的数量  */
@property (nonatomic,assign) NSInteger buyNumber;

@property (nonatomic,strong) CNGoodsModel* count;

@property (nonatomic,strong) CNShopBuyView* buyView;


@end

@implementation CNShopBuyView

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    _addGoodsButton = [[UIButton alloc]init];
    [_addGoodsButton setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [_addGoodsButton addTarget:self action:@selector(addButtonCliked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addGoodsButton];
    
    _reduceGoodsButton = [[UIButton alloc]init];
    [_reduceGoodsButton setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    [_reduceGoodsButton addTarget:self action:@selector(reduceButtonCliked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_reduceGoodsButton];
    
    _countLabel = [[UILabel alloc]init];
    _countLabel.text = @"0";
    _countLabel.font = [UIFont systemFontOfSize:14];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_countLabel];
    

    [_reduceGoodsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(-25);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(self.mas_height);
    }];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_reduceGoodsButton.mas_trailing).offset(3);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.trailing.equalTo(_addGoodsButton.mas_leading).offset(-2);
    }];
    [_addGoodsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self).offset(25);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.width.mas_equalTo(self.mas_height);
    }];

    
    return self;
}
#pragma mark 购买商品的set方法
- (void)setGoods:(CNGoodsModel *)goods{
    _goods = goods;
//    if(goods .count <= 0){
//        [self setSupplementLabelShow:YES];
//    }else{
//         [self setSupplementLabelShow:NO];
//    }
    //显示购买的数量
    self.buyNumber = goods.count;
}
#pragma mark 购买商品数量的方法
- (void)setBuyNumber:(NSInteger)buyNumber{
    _buyNumber = buyNumber;
    //不显示减号
    if(self.zeroNeverShow){
        //按钮隐藏
        self.reduceGoodsButton.hidden = YES;
        //计数的label隐藏
        self.countLabel.hidden = YES;
    }else{
        if(buyNumber == 0){
            //减少按钮隐藏 和显示0
            self.reduceGoodsButton.hidden =YES && !self.zearIsShow;
            //计数的lable隐藏
            self.countLabel.hidden = YES && !self.zearIsShow;
        }else{
            //让countLable显示购买数量 buyNumber
            self.countLabel.text = [NSString stringWithFormat:@"%ld",(long)buyNumber];
            self.reduceGoodsButton.hidden = NO;
            //计数的label隐藏
            self.countLabel.hidden = NO;
        }
        
    }
    
}
#pragma mark 库存没货的方法
- (void)setSupplementLabelShow:(BOOL)show{
    if (show) {
        //让缺少库存的label显示 其他lable和按钮隐藏
        self.supplementLabel.hidden = NO;
        self.countLabel.hidden= YES;
        self.addGoodsButton.hidden = YES;
        self.reduceGoodsButton.hidden = YES;
    }else{
        //让缺少库存的按钮隐藏 其他控件显示
        self.supplementLabel.hidden = YES;
        self.countLabel.hidden= NO;
        self.addGoodsButton.hidden = NO;
        self.reduceGoodsButton.hidden = NO;
    }
}

#pragma mark 加号按钮的点击事件
- (void)addButtonCliked:(UIButton *)button{
    NSLog(@"点击了加号");
    //首先判断如果购买的数量大于商品的数量 接收通知并返回
//    if(self.buyNumber >= self.goods.number){
//        [[NSNotificationCenter defaultCenter]postNotificationName:CNShopCarBuyNumberDidChangeNotification object:self.goods.name];
//        return;
//    }
     //点击商品数量增加
//    self.buyNumber++;
//    // 都是NSInterger类型的购买商品的数量赋值给商品添加的次数
//    self.goods.userBuyNumber = self.buyNumber;

    //调用单例来添加商品 并接收通知
    // [[CNUserShopCarTools sharedInstance] addSuperMarkProductToShopCar:self.goods];
    //显示购买商品的数量

 
    [[NSNotificationCenter defaultCenter]postNotificationName: CNShopCarBuyNumberDidChangeNotification object:nil];
    if (_goods.count >= 0) {
        self.goods.count ++;
        [[SQLiteManager sharedSQLiteManager]charu:_goods With: +1];
        self.countLabel.text = [NSString stringWithFormat:@"%d",self.goods.count];
       
    }else if(_goods.count == 0){
        self.buyView.hidden = YES;
        self.tableView.hidden = YES;
    }
    [self.tableView reloadData];
}

#pragma mark 减号按钮的点击事件
- (void)reduceButtonCliked:(UIButton *)button{
    NSLog(@"点击了减号");
    //并接收通知
    [[NSNotificationCenter defaultCenter]postNotificationName: CNShopCarBuyNumberDidChangeNotification object:nil];
    if (_goods.count > 0) {
        self.goods.count --;
        
        self.countLabel.text = [NSString stringWithFormat:@"%d",self.goods.count];
        [[SQLiteManager sharedSQLiteManager]charu:_goods With:-1];
    }else if(_goods.count == 0){
        self.buyView.hidden = YES;
        self.tableView.hidden = YES;
    }
       [self.tableView reloadData];
    
    CNTabBarController *tab =(CNTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    CNNavViewController *nav = tab.childViewControllers[2];
     CNShoppingViewController * shopingVC = nav.childViewControllers.firstObject;
    
    shopingVC.shopCar = [[SQLiteManager sharedSQLiteManager] queryContact];
    for (UIView *v in shopingVC.view.subviews) {
        
        if ([v isKindOfClass:[UITableView class]]) {
            UITableView *tab =  (UITableView *)v;
            [tab reloadData];
        }
       
    }
    
}
//    //首先判断如果购买的数量大于商品的数量 接收通知并返回
//    if(self.buyNumber <= self.goods.number){
//        return;
//    }
//    //购买的商品数量减少
//    self.buyNumber--;
//    // 将购买减少的商品的数量赋值给商品的数量
//    self.goods.userBuyNumber = self.buyNumber;
//    if(self.buyNumber == 0 ){
//    //[[CNUserShopCarTools sharedInstance] addSuperMarkProductToShopCar:self.goods];
//    }else{
//        //显示购买商品的数量
//        self.countLabel.text = [NSString stringWithFormat:@"%ld",self.buyNumber];
//    }
@end
